package aeolus.wind.spider.web.commons.spider;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class SomeGroup {

    /**
     * 把group看成一个人，每添加一个成员就是改成员对它的打分。如果有最大值则是给自己打分。
     */
    private SomeBody                groupBody;
    /**
     * 处理处于各能力顶点的成员
     */
    private Map<String, MaxInGroup> maxInGroupMap;

    public SomeGroup() {
        groupBody = new SomeBody("groupBody");
        maxInGroupMap = new HashMap<String, MaxInGroup>();
    }

    public void addMember(SomeBody member) {
        Map<String, Double> memberMap = member.getMeInOther();// 他人眼中的人才是有价值的人
        for (Entry<String, Double> entry : memberMap.entrySet()) {
            groupBody.addAbilityByMeWithMax(entry.getKey(), entry.getValue());
            groupBody.addAbilityByOther(entry.getKey(), entry.getValue());
            putMax(member.getIdentityName(), entry.getKey(), entry.getValue());
        }
    }

    public Map<String, Double> getGroupMax() {
        return groupBody.getMe();
    }

    public Map<String, Double> getGroupAve() {
        return groupBody.getMeInOther();
    }

    /**
     * 返回团队内的各能力最强的人员和得分
     * 
     * @return
     */
    public Map<String, MaxInGroup> getMaxInGroup() {
        return maxInGroupMap;
    }

    private void putMax(String identityName, String abilityName, Double value) {
        MaxInGroup maxInGroup = maxInGroupMap.get(abilityName);
        if (maxInGroup == null) {
            maxInGroup = new MaxInGroup(abilityName);
            maxInGroupMap.put(abilityName, maxInGroup);
        }
        maxInGroup.update(identityName, value);
    }
}
